#include <iostream>
#include <pthread.h>
#include <ctime>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include "BlockingQueue.hpp"
#include "Task.hpp"
using std::cerr;
using std::cout;
using std::endl;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

template <class C, class S> // C:计算，S：存储
class BlockQueues
{
public:
    BlockingQueue<C> *c_bp;
    BlockingQueue<S> *s_bp;
};

void *producter(void *args)
{
    // BlockQueues<CalTask, SaveTask> *bq = static_cast<BlockQueues<CalTask, SaveTask> *>(args);
    BlockingQueue<CalTask> *bq = (static_cast<BlockQueues<CalTask, SaveTask> *>(args))->c_bp;
    while (1)
    {

        // 生产
        int x = rand() % 10 + 1;
        // int y = rand() % 5 + 1;
        int y = rand() % 5; // 不加1可能会%到0
        char op = rand() % oper.size();
        CalTask t(x, y, oper[op], my_math);
        bq->push(t);
        pthread_mutex_lock(&mutex);
        cout << "producter thread 生产计算任务 : " << t.TaskToString() << endl;
        pthread_mutex_unlock(&mutex);
        sleep(1);
        //   cout << "生产数据 : " << t << endl;
        //// sleep(1);//生产速度慢下来，消费数据也必须慢，因为这是阻塞队列
    }
    return nullptr;
}

void *consumer(void *args)
{
    // BlockQueues<CalTask, SaveTask> *bq = static_cast<BlockQueues<CalTask, SaveTask> *>(args);
    BlockingQueue<CalTask> *bp = (static_cast<BlockQueues<CalTask, SaveTask> *>(args))->c_bp;       // 2号线程从这个队列拿数据，进行消费处理
    BlockingQueue<SaveTask> *save_bp = (static_cast<BlockQueues<CalTask, SaveTask> *>(args))->s_bp; // 2号线程向这个队列放数据，给3号线程进行消费

    while (1)
    {
        // 消费 —— 计算任务
        CalTask t;
        // bq->c_bp->pop(&t);//对应BlockQueues<CalTask, SaveTask> *bq = static_cast<BlockQueues<CalTask, SaveTask> *>(args);
        bp->pop(&t);
        std::string result = t(); // 将消费处理结果，赋值给result
        pthread_mutex_lock(&mutex);
        cout << "consumer thread 消费计算任务 : " << result << endl; // 仿函数 ->（）
        pthread_mutex_unlock(&mutex);

        // 生产 —— 存储任务
        SaveTask save(result, savethread);
        save_bp->push(save);
        pthread_mutex_lock(&mutex);
        cout << "推送保存任务完成..." << endl; // 仿函数 ->（）
        pthread_mutex_unlock(&mutex);
        // sleep(1);
    }
    return nullptr;
}

void *saver(void *args)
{
    BlockingQueue<SaveTask> *save_bp = (static_cast<BlockQueues<CalTask, SaveTask> *>(args))->s_bp;
    while (1)
    {
        SaveTask t;
        save_bp->pop(&t);
        t();
        cout << "saver thread 保存任务完成..." << endl; // 仿函数 ->（）
    }
    return nullptr;
}

int main()
{
    srand((unsigned long)time(nullptr) ^ getpid());
    BlockQueues<CalTask, SaveTask> bps;

    bps.c_bp = new BlockingQueue<CalTask>();
    bps.s_bp = new BlockingQueue<SaveTask>();
    // BlockingQueue<CalTask> *task_bq = new BlockingQueue<CalTask>();//两个阻塞队列
    // BlockingQueue<SaveTask> *save_bq = new BlockingQueue<SaveTask>();

    pthread_t c, p, s;
    pthread_create(&p, nullptr, producter, &bps);
    pthread_create(&c, nullptr, consumer, &bps);
    pthread_create(&s, nullptr, saver, &bps);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);
    pthread_join(s, nullptr);

    delete bps.c_bp;
    delete bps.s_bp;
    return 0;
}